export default class HttpClient {
    static instance;
    RootUrl = process.env.NODE_ENV === "development"
        ? "/cpasApi"
        : localStorage.getItem("cpasApiUrl");
    constructor() { }
    static getInstance() {
        if (!this.instance) {
            this.instance = new HttpClient();
        }
        return this.instance;
    }
    //todo 完整参数封装
    async fetchGet(url) {
        try {
            const response = await fetch(this.RootUrl + url);
            return response.json();
        }
        catch (e) {
            throw e;
        }
    }
    async fetchPost(url, params) {
        try {
            const response = await fetch(this.RootUrl + url, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify(params),
            });
            return response.json();
        }
        catch (e) {
            throw e;
        }
    }
}
/**
 * 获取 sql 模板接口数据
 * @param apiName api名称
 * @param params 发送参数，一般都为 JSON格式
 * @returns ServerResult
 */
export async function getApiData(apiName, params) {
    const pd = {
        name: apiName,
    };
    if (params) {
        pd.params = params;
    }
    try {
        const res = await HttpClient.getInstance().fetchPost(`/sqlapis/getApiData`, pd);
        if (res.code !== 0) {
            // console.log(`请求返回错误,code为：${res.code}`);
        }
        return res;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
